<?php

/**
 * This is the model class for table "memberImage".
 *
 * The followings are the available columns in table 'memberImage':
 * @property integer $_id
 * @property integer $insertTime
 * @property integer $updateTime
 * @property integer $ownerId
 * @property integer $memberId
 * @property string $name
 * @property string $filename
 * @property string $extension
 * @property integer $size
 * @property string $type
 */
class MemberImage extends ActiveRecord {

	const NAME_PHOTO = 'Foto';
	const NAME_AVATAR = 'Avatar';

	public $file;

	/**
	 * Returns the static model of the specified AR class.
	 * @return MemberImage the static model class
	 */
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'memberImage';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'file',
				'file',
				'types'=>'jpg, gif, png',
				'maxSize'=>10485760,
				'allowEmpty'=>Yii::app()->params['mode'] == 'test',
				'on'=>'insert',
			),
			array(
				'file',
				'file',
				'types'=>'jpg, gif, png',
				'maxSize'=>10485760,
				'on'=>'update',
				'allowEmpty'=>true,
			),
			array('ownerId', 'numerical'),
			array('name', 'required'),
			array('name', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('name', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'name'=>'Name',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('name', $this->name, true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

	public function getNameList() {
		return array(
			self::NAME_PHOTO,
			self::NAME_AVATAR,
		);
	}

	/**
	 * @return boolean
	 */
	protected function beforeSave() {
		if (!parent::beforeSave()) return false;
		$this->file = CUploadedFile::getInstance($this, 'file');
		if ($this->file) {
			$this->filename = $this->file->name;
			$this->extension = strtolower($this->file->extensionName);
			$this->size = $this->file->size;
			$this->type = $this->file->type;
		}
		return true;
	}

	/**
	 * If scenario is insert then save the uploaded image
	 */
	protected function afterSave() {
		parent::afterSave();
		if ($this->file) {
			// We must save the uploaded file here because path depends on _id
			// and _id only exist after saving.
			$folder = Yii::app()->params['image']['member'];
			if (!is_dir("$folder/$this->folder")) {
				mkdir("$folder/$this->folder");
			}
			$path = "$folder/$this->path";
			$this->file->saveAs($path);
		}
	}

	/**
	 * Delete image if exists
	 */
	protected function afterDelete() {
		parent::afterDelete();
		$folder = Yii::app()->params['memberImage'];
		$path = "$folder/$this->path";
		if (is_file($path)) unlink($path);
	}

	/**
	 * @property integer number of subfolder of the id
	 */
	public function getFolder() {
		return intval($this->_id / 1000);
	}

	/**
	 * @property string $path absolute location of the image file
	 */
	public function getPath() {
		return "$this->folder/$this->_id.$this->extension";
	}

}